package practicedemo21;

import java.util.Random;

public class practicedemo21 {
    //定义方法实现随机产生一个五位验证码
    //要求：长度位5 前四位是大写字母或者小写字母 最后一位是数字

    public static void main(String[] args){
        System.out.print("生成的验证码是：");
        System.out.println(random(5));
    }
    public static char[] random(int x){
        //静态分配数组
        char array1[] = new char[] {'a','A','b','B','c','C','d','D','e','E','f','F','g','G','h','H','i','I','j','J','k','K','l','L','m','M',
                'n','N','o','O','p','P','q','Q','r','R','s','S','t','T','u','U','v','V','w','W','x','X','y','Y','z','Z'};
        char array2[] = new char[] {'0','1','2','3','4','5','6','7','8','9'};
        //动态分配数组
        char array3[] = new char[x];

        //随机生成
        //公式：
        // Random rand = new Random();
        // int randNumber = rand.nextInt(MAX - MIN + 1) + MIN;
        int index;
        Random rand = new Random();
        for (int i = 0; i < x; i++) {
            index = rand.nextInt(52);
            array3[i] = array1[index];
            if (i==4){
                index = rand.nextInt(10);
                array3[i] = array2[index];
            }
        }
        //返回数组
        return array3;
    }
}
